package com.example.demo.component;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.example.demo.entity.EO;
import com.example.demo.entity.SysTaskLog;
import com.example.demo.mapper.*;
import com.example.demo.service.EoService;
import com.example.demo.utils.DateTransferUtil;
import com.example.demo.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EoSynchroniseTask {
    @Autowired
    EoMapper eoMapper;
    @Autowired
    SysTaskLogMapper taskLogMapper;

//    @Scheduled(cron = "0 59 00 * * ?")
//    public void synchronise() {
//        String nowTime = DateTransferUtil.getNow();
//
//        Connection connection;
//
//        try { // 加载数据库驱动类
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            System.out.println("数据库驱动加载成功");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        String user = "gh001";//数据库登录名
//        String password = "zjgh2023";//密码
//
//        try {
//            connection = DriverManager.getConnection("jdbc:mysql://172.16.40.110:3306/contractual_service?useUnicode=true&characterEncoding=gbk", user, password);
//            System.out.println("数据库连接成功");
//            Statement statement = connection.createStatement();
//            String sql = "SELECT * FROM quotation;";
//            ResultSet resultSet = statement.executeQuery(sql);
//
//            while(resultSet.next()){
//                String id = resultSet.getString("id");
//                EO eo1 = eoMapper.selectByQuatationId(id);
//
//                if (eo1 == null) {
//                    EO eo = new EO();
//                    eo.setQuatationId(id);
//                    eo.setName(resultSet.getString("con_name"));
//                    eo.setTitle(eo.getName());
//                    eo.setStartTime(transferFormat(resultSet.getString("con_start_date")));
//                    eo.setEndTime(transferFormat(resultSet.getString("con_end_date")));
//                    eo.setGuestNum(resultSet.getInt("con_user_count"));
//                    eo.setGuestName(resultSet.getString("unit_name"));
//                    eo.setIsActive(resultSet.getBoolean("is_active"));
//                    eo.setOrigin("场馆运营系统同步");
//                    eo.setType("活动");
//                    eo.setCreateTime(nowTime);
//                    eo.setCreateUser("自动同步");
//                    eo.setPeriod(getPeriod(eo.getStartTime(), eo.getEndTime()));
//                    if (nowTime.compareTo(eo.getEndTime()) > 0 ){
//                        eo.setStatus("已完成");
//                    } else if(nowTime.compareTo(transferFormat(resultSet.getString("con_start_date"))) >=0
//                            && nowTime.compareTo(transferFormat(resultSet.getString("con_end_date"))) <=0 ){
//                        eo.setStatus("执行中");
//                    } else {
//                        eo.setStatus("未编辑");
//                    }
//                    eoMapper.insert(eo);
//                } else {
//                    if (StrUtil.isNotEmpty(eo1.getStatus()) && !eo1.getStatus().equals("未编辑")) {
//                        continue;
//                    }
//                    eo1.setQuatationId(id);
//                    eo1.setName(resultSet.getString("con_name"));
//                    eo1.setTitle(eo1.getName());
//                    eo1.setStartTime(transferFormat(resultSet.getString("con_start_date")));
//                    eo1.setEndTime(transferFormat(resultSet.getString("con_end_date")));
//                    eo1.setGuestNum(resultSet.getInt("con_user_count"));
//                    eo1.setGuestName(resultSet.getString("unit_name"));
//                    eo1.setIsActive(resultSet.getBoolean("is_active"));
//                    eo1.setOrigin("场馆运营系统同步");
//                    eo1.setType("活动");
//                    eo1.setCreateTime(nowTime);
//                    eo1.setCreateUser("自动同步");
//                    eo1.setPeriod(getPeriod(eo1.getStartTime(), eo1.getEndTime()));
//                    if (nowTime.compareTo(eo1.getEndTime()) > 0 ){
//                        eo1.setStatus("已完成");
//                    } else if(nowTime.compareTo(transferFormat(resultSet.getString("con_start_date"))) >=0
//                            && nowTime.compareTo(transferFormat(resultSet.getString("con_end_date"))) <=0 ){
//                        eo1.setStatus("执行中");
//                    } else {
//                        eo1.setStatus("未编辑");
//                    }
//                    eoMapper.updateById(eo1);
//                }
//            }
//            resultSet.close();
//            statement.close();
//
//            List<EO> eos = eoMapper.selectList(null);
//            PreparedStatement preparedStatement = null;
//            ResultSet resultSet1 = null;
//            String sql1 = "SELECT * FROM quotationvenueassociation WHERE is_active = 1 and quotation_id = ?;";
//            for(EO item : eos){
//                preparedStatement = connection.prepareStatement(sql1, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//                preparedStatement.setString(1, item.getQuatationId());
//                resultSet1 = preparedStatement.executeQuery();
//                if(resultSet1.first()){
//                    item.setLocation(resultSet1.getString("venue_name"));
//                    eoMapper.updateById(item);
//                }
//            }
//            resultSet1.close();
//            preparedStatement.close();
//            connection.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        SysTaskLog log = new SysTaskLog();
//        log.setResult("success");
//        log.setTime(nowTime);
//        taskLogMapper.insert(log);
//    }

//    @Scheduled(cron = "0 00 08 * * ?")
//    public void sendSms() {
//        String nowTime = DateTransferUtil.getNow();
//
//        SysTaskLog log = taskLogMapper.getNewest();
//        if (log.getTime().substring(0, 10).equals(nowTime.substring(0, 10))){
//            if (log.getResult().equals("success")) {
//                JSONArray phoneNumber = new JSONArray();
//                phoneNumber.add("13777067965");
//                JSONArray sign = new JSONArray();
//                sign.add("宁波国际会议中心");
//                JSONArray param = new JSONArray();
//                JSONObject object = new JSONObject();
//                object.set("name", "test");
//                object.set("activity", "场馆运行系统订单同步");
//                param.add(object);
//                String res = SMSUtil.sendBatch(phoneNumber.toString(), sign.toString(), param.toString());
//            }
//        }
//    }

    public static String transferFormat(String inTime) throws ParseException {
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date tempDate;
        String outTime;
        tempDate = s1.parse(inTime);
        outTime = s2.format(s2.parse(s1.format(tempDate)));
        return outTime;
    }

    public static Long getPeriod(String start, String end){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long d = 0L;
        try{
            long d1 = format.parse(start).getTime();
            long d2 = format.parse(end).getTime();
            d = (d2 - d1)/(1000 * 60 * 60 * 24);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return d+1;
    }
}
